<template>
  <div class="top-category">
    <div class="container">
      <!-- 动画
        条件：进入、离开
        使用：
          1：用transition包住
          2:加name="后面css的前缀xxx"
            .xxx-enter-from:进入前状态
            .xxx-leave-to:离开时状态处理
            .xxx-enter-active:进入过程中的处理（动画的执行体）
            .xxx-leave-active:离开过程中的处理（动画的执行体）
            mode:模式 默认是同时执行，可以调整先后  out  出去  in: 进入
            transition
              a  v-if="bol"
              b  v-else
             mode:先消失再显示   

             ul
                li:张三  key=1

             ul
                li:李四  key=2
             diff:找不同，如果有key值，就要找key值对应的项
             先删除张三，再新增李四
         -->
      <XtxBread>
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <transition name="xxx" mode="out-in">
          <XtxBreadItem :key="info.id">{{ info.name }}</XtxBreadItem>
        </transition>
      </XtxBread>
      <div style="height:500px">
        <XtxCarousel :sliders="list" />
      </div>
      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="i in info.children" :key="i.id">
            <a href="javascript:;">
              <img :src="i.picture" />
              <p>{{ i.name }}</p>
            </a>
          </li>
        </ul>
      </div>
      <!-- 分类商品 -->
      <!-- 分类关联商品 -->
      <div class="ref-goods" v-for="item in categoryList" :key="item.id">
        <div class="head">
          <h3>- {{ item.name }} -</h3>
          <p class="tag">{{ item.saleProperties }}</p>
          <XtxMore />
        </div>
        <div class="body">
          <GoodsItem :goods="i" v-for="i in item.goods" :key="i.id" />
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import { useStore } from 'vuex'
import { useRoute, onBeforeRouteLeave } from 'vue-router'
import { computed, onBeforeUnmount, onUnmounted, ref, watch } from 'vue'
// import { homeBanner } from '@/api/home'
import GoodsItem from './components/goods-item.vue'
import { category } from '@/api/category'
export default {
  components: {
    GoodsItem
  },
  setup () {
    const store = useStore()
    const route = useRoute()
    // 依赖一个或者多个值产生一个新的值
    // 找到当前项id对应的数据
    const info = computed(() => {
      const temp = store.state.category.list.find(
        item => item.id === route.params.id
      )
      // return temp ? temp : ''
      return temp || ''
    })
    // 轮播图
    const list = ref([])
    store.dispatch('home/getSliderList').then(res => {
      list.value = res
    })
    // homeBanner().then(res => {
    //   list.value = res.result
    //   console.log('图片', res)
    // })
    // 一级公类数据处理
    /*
    watch(需要观察的值（原始值()=>原始值）,引用值可以直接写,()=>{},{
      deep:是否尝试监听,
      immediate:第一次监听就执行,
      flush:
         pre:在渲染前执行回调
         post:在渲染后执行回调
         sync:修改一次就立马执行一次回调
    })
    
    */
    const categoryList = ref([])
    const stop = watch(
      () => route.params.id,
      () => {
        category(route.params.id).then(res => {
          // console.log('一级分类:', res)
          categoryList.value = res.result.children
        })
      },
      {
        immediate: true
      }
    )
    // 路由离开前停止watch
    onBeforeRouteLeave((to, from) => {
      // console.log('onBeforeRouteLeave')
      stop()
    })
    onBeforeUnmount(() => {
      // console.log('onBeforeUnmount')
    })
    onUnmounted(() => {
      // console.log('onUnmounted')
    })

    return { info, list, categoryList }
  }
}
</script>
<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
}
.ref-goods {
  background-color: #fff;
  margin-top: 20px;
  position: relative;
  .head {
    .xtx-more {
      position: absolute;
      top: 20px;
      right: 20px;
    }
    .tag {
      text-align: center;
      color: #999;
      font-size: 20px;
      position: relative;
      top: -20px;
    }
  }
  .body {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    padding: 0 65px 30px;
  }
}
.xxx-enter {
  &-from {
    opacity: 0;
    transform: translateX(150px);
  }
  &-active {
    transition: all 0.5s;
  }
}
.xxx-leave {
  &-to {
    opacity: 0;
    transform: translateX(150px);
  }
  &-active {
    transition: all 0.5s;
  }
}
</style>
